//查询是否被预约了
$(function (){
    var waresNo = $("#waresNo").text();
    var username = $("#username").text();
    console.log(waresNo);
    console.log(username);
    findStatus(waresNo,username);
    appointmentCount(waresNo,username);

    $("#appointment").click(function () {
        //每次点击都可以更改预约状态
        changeAppointmentStatus(waresNo,username);
    });

});

//查看状态
function findStatus(waresNo,username) {
    $.ajax({
        url:"/user/wares/appointment",
        data:{
            waresNo:waresNo,
            username:username
        },
        type:"PUT",
        success:function (resultInfo) {
            if(resultInfo.flag){
                //改变预约按钮样式
                $("#appointment").addClass("already");
                $("#appointment").attr("disabled","disabled");
                $("#appointment").text("取消预约");
            }else {
                $("#appointment").text("点击预约");
            }
        }
    })
}
//被预约了多少次？
function appointmentCount(waresNo,username) {
    $.ajax({
        url:"/user/wares/waresAppointmentCount",
        data:{
            waresNo:waresNo,
        },
        type:"PUT",
        success:function (count) {
            $("#appointmentCount").text(count);
        }
    })
}
//改变预约状态
function changeAppointmentStatus(waresNo,username) {
    $.ajax({
        url:"/user/wares/changeAppointment",
        data:{
            waresNo:waresNo,
            username:username
        },
        type:"PUT",
        success:function (resultInfo) {
            if(resultInfo.flag){
                alert(resultInfo.message);
            }else {
                alert(resultInfo.message);
            }
            //刷新页面
            location.reload();
        }
    })
}